//
//  ProvienceViewController.m
//  QuickCure37
//
//  Created by 马文星 on 16/6/19.
//  Copyright © 2016年 Demos. All rights reserved.
//

#import "ProvienceViewController.h"
#import "ProvienceDTO.h"
#import "CityViewController.h"



@interface ProvienceViewController () <UITableViewDataSource,UITableViewDelegate>


@property (nonatomic, strong) UITableView *tableview;

/** dto 数组 */
@property (nonatomic, strong) NSArray *dtoArray;

@end

@implementation ProvienceViewController

static NSString  *ID = @"ProvienceID";

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setupUI];
    [self loadData];
    
}

- (void)setupUI{
    
    self.title = @"选择当前省份位置";

    /*!
     *  添加控件
     */
    [self.view addSubview:self.tableview];
    [self.tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:ID];
    
    /*!
     *  添加约束
     */
    
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(0);
        make.left.right.bottom.mas_equalTo(0);
        
    }];
    

}

- (void)loadData{

    NSString *path = [[NSBundle mainBundle] pathForResource:@"province.plist" ofType:nil];
    
    NSURL *url = [NSURL fileURLWithPath:path];
    
    NSArray *arr = [NSArray arrayWithContentsOfURL:url];
    
    self.dtoArray = [ProvienceDTO dtoWithArray:arr];
    
}


#pragma mark  - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.dtoArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];
    
    ProvienceDTO *dto = self.dtoArray[indexPath.row];
    cell.textLabel.text = dto.name;
    
    return cell;
}


#pragma mark  - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    ProvienceDTO *dto = self.dtoArray[indexPath.row];
    CityViewController *cityVC = [[CityViewController alloc] init];

    cityVC.clickCityBlock = self.clickCityBlock;
    cityVC.cities = dto.cities;
    [self.navigationController pushViewController:cityVC animated:YES];
}


#pragma mark  - Lazy
-(UITableView *)tableview{
    
    if (_tableview == nil) {
        _tableview = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableview.dataSource = self;
        _tableview.delegate = self;
    }
    return _tableview;

}

@end
